package com.woniuxy.shiro;


import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//过滤器
@Component
public class JwtFilter extends BasicHttpAuthenticationFilter {

//    Spring的拦截器:preHandle->controller->postHandler-->view --> afterHandler
//    这个preHandle通常用来处理跨域的，全局跨域处理
//    重要的过滤器配置：需要我们自己开发，获取请求头中的信息，然后执行subject.login(new MyJsonWebToken())
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin"));
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
        return super.preHandle(request, response);
    }

//     做条件判断，是否允许访问后端，也就是看你请求头有没有带上token
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        //判断请求头有没有信息
        if (isLoginAttempt(request,response)){
            try {
                //执行登录操作----在下面
                executeLogin(request,response);
            } catch (Exception e) {
                response401(response);
                return false;
            }
        }else {
            // 如果请求头中没有token ，我们应该跳转后台的controller返回一个信息给前端，告诉他还没有登录 401 没有权限
            return false;
        }
        return true;
    }

//     重写shiro底层过滤器流程：这个方法主要是判断请求头中有没有token信息
    @Override
    protected boolean isLoginAttempt(ServletRequest request, ServletResponse response) {
        HttpServletRequest request1 = (HttpServletRequest) request;
        //从请求头中获取token信息，Authorization 就是自定义的key！
        String token = request1.getHeader("Authorization");
        return token == null?false:true;
    }

//     需要执行的是我们自定义的MyJsonWebToken 实体类的校验
    @Override
    protected boolean executeLogin(ServletRequest request, ServletResponse response) {
        HttpServletRequest request1 = (HttpServletRequest) request;
        //从请求头中获取token信息，Authorization 就是自定义的key！
        String token = request1.getHeader("Authorization");
        Subject subject = getSubject(request, response);
        MyJsonWebToken myJsonWebToken = new MyJsonWebToken(token);
        subject.login(myJsonWebToken);
        return true;
    }

    public void response401(ServletResponse response){
        try {
            HttpServletResponse response1 = (HttpServletResponse) response;
            response1.sendRedirect("/401");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
